#include "MyString.h"

MyString::MyString()
{
	m_len = 0;
	m_p = new char[m_len + 1];
	strcpy(m_p, "");
}


MyString::~MyString()
{
	if (m_p != NULL) {
		delete[] m_p;
		m_p = NULL;
		m_len = 0;
	}
}

MyString::MyString(const char* p) {
	if (p == NULL) {
		m_len = 0;
		m_p = new char[m_len + 1];
		strcpy(m_p, "");
	}
	else {
		m_len = strlen(p);
		m_p = new char[m_len + 1];
		strcpy(m_p, p);
	}
};
MyString::MyString(const MyString& s) {
	m_len = s.m_len;
	m_p = new char[m_len + 1];
	strcpy(m_p,s.m_p);
};

MyString& MyString::operator= (const char *p) {
	//旧内存释放掉
	if (m_p != NULL) {
		delete[] m_p;
		m_len = 0;
	}
	//根据p分配内存
	if (p == NULL) {
		m_len = 0;
		m_p = new char[m_len + 1];
		strcpy(m_p, "");
	}
	else {
		m_len = strlen(p);
		m_p = new char[m_len + 1];
		strcpy(m_p,p);

	}

	return *this;

};
MyString& MyString::operator= (const MyString &s) {
	//旧内存释放掉
	if (m_p != NULL) {
		delete[] m_p;
		m_len = 0;
	}
	//根据s分配内存
	
	m_len = s.m_len;
	m_p = new char[m_len + 1];
	strcpy(m_p, s.m_p);

	
	return *this;
};

char& MyString::operator[] (int index) {
	return m_p[index];
};

bool MyString::operator== (const char *p) {
	if (p == NULL) {
		if (m_len == 0) {
			return true;
		}
		else {
			return false;
		}
	}
	else {
		if (m_len == strlen(p)) {
			return !strcmp(m_p,p);
		}
		else {
			return false;
		}
	}
	
}
bool MyString::operator!= (const char *p) {
	return !(*this == p);
}

bool MyString::operator== (const MyString &p) {
	
	if (m_len != p.m_len) {
		return false;
	}
	return !strcmp(m_p, p.m_p);
}



bool MyString::operator!= (const MyString &p) {
	return !(*this == p);
}

int MyString::operator< (const char *p) {
	return strcmp(this->m_p, p);
};
int MyString::operator> (const char *p) {
	return strcmp(p, this->m_p);
};
int MyString::operator< (const MyString& s) {
	return strcmp(this->m_p, s.m_p);
};
int MyString::operator> (const MyString& s) {
	return strcmp(s.m_p,m_p);
};

ostream& operator<<(ostream& out, MyString &s) {
	out << s.m_p;
	return out;
}